package com.ztesoft.book.core.base.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztesoft.book.common.utils.context.ShiroContextUtil;
import com.ztesoft.book.common.utils.exception.ServiceException;
import com.ztesoft.book.core.base.base.mapper.BaseExMapper;
import com.ztesoft.book.core.base.base.service.BaseExService;
import com.ztesoft.book.core.sys.sysuser.dto.SysUserDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;


/**
 * @Description 通用Service
 * @Author YangFan
 * @Date 2021-06-22 17:29
 */
public abstract class BaseExServiceImpl<M extends BaseExMapper<T, Q>, T, Q> extends ServiceImpl<M, T> implements BaseExService<T, Q> {

    /**
     * 方法功能描述:
     * < 获取对象Mapper >
     *
     * @return M                            【返回值 注释说明】
     * @author YangFan
     * @date 2021-06-24 22:48
     */
    protected abstract M getMapper();


    /**
     * 方法功能描述:
     * < 该方法在API项目中无效，会报错，因为API项目并未引入Shiro >
     *
     * @return SysUserDTO                    【返回值 注释说明】
     * @author YangFan
     * @date 2021-06-30 07:26
     */
    @Override
    public SysUserDTO getShiroUser() {
        String jsonstr = ShiroContextUtil.getShiroCacheUser();
        if (StringUtils.isNotBlank(jsonstr)) {
            SysUserDTO userdto = null;
            try {
                userdto = JSON.parseObject(jsonstr, SysUserDTO.class);
            }
            catch (Exception e) {
                log.error("ShiroCacheUser 解析失败！");
            }
            return userdto;
        }
        else {
            return null;
        }
    }


    @Override
    public T findById(String id) {
        return getBaseMapper().selectById(id);
    }


    @Override
    public T findOne(Q qo) {
        return getBaseMapper().findOne(qo);
    }


    @Override
    public List<T> findList(Q qo) {
        return getBaseMapper().findList(qo);
    }


    @Override
    public PageInfo<T> findPage(Q qo, final Integer current, final Integer pageSize) {
        if (current != null && pageSize != null) {
            PageHelper.startPage(current, pageSize, true);
        }
        List<T> list = getMapper().findList(qo);
        if (CollUtil.isEmpty(list)) {
            list = Collections.emptyList();
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }


    @Override
    public void saveObject(T t) {
        beforeAdding(t);
        int num = getMapper().insert(t);
        if (num < 1) {
            throw new ServiceException("新增失败！");
        }
        afterAdding(t);
    }


    @Override
    public void updateObject(T t) {
        beforeUpdating(t);
        int num = getMapper().updateById(t);
        if (num < 1) {
            throw new ServiceException("修改失败！");
        }
        afterUpdating(t);
    }


    @Override
    public void deleteObject(String id) {
        beforeDeleting(id);
        int num = getMapper().deleteById(id);
        if (num < 1) {
            throw new ServiceException("删除失败！");
        }
        afterDeleting(id);
    }


    @Override
    public void fakeDeleteByPrimaryKey(String id) {
        beforeDeleting(id);
        int num = getMapper().fakeDeleteByPrimaryKey(id);
        if (num < 1) {
            throw new ServiceException("删除失败！");
        }
        afterDeleting(id);
    }


    protected void beforeAdding(T t) {

    }

    protected void afterAdding(T t) {

    }

    protected void beforeUpdating(T t) {

    }

    protected void afterUpdating(T t) {

    }

    protected void beforeDeleting(String id) {

    }

    protected void afterDeleting(String id) {

    }

}
